<?php

class Default_Model_ThuChi extends Henry_Db_Table 
{
	static private $TBNAME = "thuchi";
	protected $_name = "thuchi"; 
  	protected $_primary = 'thuchi_id';
  	const THU = "-";
  	const CHI = "+";
  	public function init()
  	{
  		$this->_name = parent::PREFIX.$this->_name;
  	}
  	public function listThuchibyDate($date)
  	{
  		if (!($date instanceof Zend_Date))
  		{
  			$date = new Zend_Date($date);
  		}
  		$select = $this->_db->select();
  		$select->from(array('tc'=>$this->_name),array('*'))
  			   ->joinInner(array('c' => Default_Model_Customer::getTBName()),'tc.customer_id = c.customer_id',array('c.customer_fname'))
  			   ->where("tc.ngay between '{$date->get('Y-M-d')} 00:00:00' and '{$date->get('Y-M-d')} 23:59:59'")
  			   ->ORDER(array('tc.customer_id','tc.ngay'))
  		;
  		return $this->_db->fetchAll($select);
  	}
  	public function layKhoangThuChi($id)
  	{
  		$select = $this->_db->select();
  		$select->from(array($this->_name),array('khoangthuchi'))
  		       ->where("thuchi_id = ?",$id);
  		return $this->_db->fetchOne($select);       
  	}
	public function luuThuChi($data)
	{
		//Chuan bi data dua vao DB
		$dateTmp = new Zend_Date();
		$ngay = $data['ngay'];
		$dateTmp->setDay($ngay->getDay());
		$dateTmp->setMonth($ngay->getMonth());
		$dateTmp->setYear($ngay->getYear());
		if ($data['phanloai'] != self::THU && $data['phanloai'] != self::CHI ) die('Không rõ thu hay chi. Nếu có lỗi này xin vui lòng liên hệ CHAN');
		$sqlData = array(
			'customer_id' => $data['customer_id'],
			'ngay'  => $dateTmp->get("Y-M-d H:m:s"),
			'khoangthuchi'=> (double)($data['phanloai'].$data['khoangthuchi']),
			'ghichu'=> $data['ghichu'],
		);
		
		//Khai bao transaction
		$db = $this->_db;
		$db->beginTransaction();
		try {
			$this->insert($sqlData);
			$TongCNOb = new Default_Model_TongCongNo();
			$TongCNOb->capnhapCongNo($sqlData['customer_id'],$sqlData['khoangthuchi']);
	    	$db->commit();
	    	return true;
		} 
		catch (Exception $e) {
		    $db->rollBack();
		    echo $e->getMessage();
		    return false;
		}
	}
	
	public static function getTBName()
	{
		return parent::PREFIX.self::$TBNAME; 
	}
	
	public function layTongThuChitheoNgay($cusID,$ngay)
	{
		$select = $this->_db->select();
		$select->from(array("ktc" => $this->_name),array('tongthuchi' => new Zend_Db_Expr("sum(ktc.khoangthuchi)")))
			   ->where("ktc.ngay between '{$ngay->get("Y-M-d")} 00:00:00' and '{$ngay->get("Y-M-d")} 23:59:59'")
			   ->where("ktc.customer_id = ?",$cusID);
		return $this->_db->fetchOne($select);	   		
	}
	public function xoaThuChi($id)
	{
		$db = $this->_db;
		$db->beginTransaction();
		try{
			$select = $this->_db->select();
			$select->from(array($this->_name),array('khoangthuchi','customer_id'))
				   ->where("thuchi_id = ?",$id);
		    $result = $this->_db->fetchRow($select);
			$tcnOb = new Default_Model_TongCongNo();
			$tcnOb->capnhapCongNo($result['customer_id'],-$result['khoangthuchi']);
			$where = $db->quoteInto("thuchi_id = ?",$id);
			$this->delete($where);
			$db->commit();
			return true;
		}
		catch(Exception $e)
		{
			$db->rollBack();
			return false;
		}
	}
	public function xoa($id)
	{
		$where = $this->_db->quoteInto("thuchi_id = ?",$id);
		$this->delete($where);		
	}	
}

?>